package com.ljkj.blockpay.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kongzue.baseokhttp.util.JsonMap;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseActivity;
import com.ljkj.blockpay.databinding.ActivityPaymentBinding;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.presenter.PaymentPresenter;
import com.ljkj.blockpay.presenter.inter.PaymentInter;
import com.ljkj.blockpay.util.ToggleToActivity;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;

import java.math.BigDecimal;
import java.util.List;

//扫码付
public class PaymentActivity extends BaseActivity<ActivityPaymentBinding, PaymentPresenter> implements PaymentInter.PaymentInterView, ClickListener {

    private String currencyName;
    private CommonDialog mConfirmDialog;
    private View minflate;
    private String originAmount;
    private String originServiceAmount;
    private String targetAccount;
    private String originCurrency;
    private String remark;

    private TextView number;
    private TextView originalamount;
    private TextView serviceAmount;
    private TextView targetAcc;
    private TextView originCurr;
    private TextView remarks;

    private CommonDialog mdropDialog;
    private CommonDialog mPasswordDialog;


    @Override
    public int initContentView() {
        return R.layout.activity_payment;
    }

    @Override
    protected PaymentPresenter createPresenter() {
        return new PaymentPresenter(this,this);
    }

    @Override
    protected void init(Bundle savedInstanceState) {


    }

    @Override
    protected void initData() {
        super.initData();
        getPresenter().networkAccurary();

        minflate = LayoutInflater.from(PaymentActivity.this).inflate(R.layout.confirmorderdialog_layout, null, false);
        number = minflate.findViewById(R.id.tv_number);
        originalamount = minflate.findViewById(R.id.tv_originalamount);
        serviceAmount = minflate.findViewById(R.id.tv_ServiceAmount);
        targetAcc = minflate.findViewById(R.id.tv_targetAccount);
        originCurr = minflate.findViewById(R.id.tv_originCurrency);
        remarks = minflate.findViewById(R.id.tv_remark);

        mdropDialog = CommonDialog.Builder.with(PaymentActivity.this).setContentView(R.layout.dropbusiness_layout)
                .setOnClickListener(R.id.btn_cancel, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ToggleToActivity.toMainActivity(PaymentActivity.this);
                    }
                })
                .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mdropDialog.dismiss();
                    }
                }).setCancelable(true).forGravity(Gravity.CENTER).create();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.et_remark:

                break;
            case R.id.btn_next:
                if (!binding.etGetmoney.getText().toString().isEmpty()){
                    binding.btnNext.setSelected(true);
                    BigDecimal bigDecimal = new BigDecimal(binding.etGetmoney.getText().toString());
                    //默认先选择第一个
                    getPresenter().orderPreconfirm(bigDecimal,4,currencyName,binding.etRemark.toString());
                    mConfirmDialog = CommonDialog.Builder.with(PaymentActivity.this).setContentView(R.layout.confirmorderdialog_layout)
                            .setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    mdropDialog.show();
                                }
                            })
                            .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    mPasswordDialog = CommonDialog.Builder.with(PaymentActivity.this).setContentView(R.layout.codeedit_layout)
                                            .setOnClickListener(R.id.iv_close, new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    mdropDialog.show();
                                                }
                                            })
                                            .setOnClickListener(R.id.btn_ok, new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                    //交易密码

                                                }
                                            }).fullWidth().setOnKeyListener((dialogInterface, i, keyEvent) -> false).setCancelable(true).create();
                                    mPasswordDialog.show();
                                    getPresenter().orderPreconfirm(bigDecimal,4,currencyName,binding.etRemark.toString());
                                }
                            }).setCancelable(true).forGravity(Gravity.BOTTOM).fullWidth().create();

                    mConfirmDialog.show();
                }
                break;
        }

    }

    //网络精度
    @Override
    public void showNetAccuracy(List<RechargeModel> modelList) {
        //默认首次进入
        getPresenter().searchCurrencyBalance(modelList.get(0).getCurrencyName(),4);
        currencyName = modelList.get(0).getCurrencyName();
        binding.tvUnitbtc.setText(currencyName);
        Glide.with(PaymentActivity.this).load(modelList.get(0).getCurrencyImg()).into(binding.ivGeticon);

    }

    //查询币种
    @Override
    public void SearchCurrencySuccess(JsonMap jsonMap) {

    }

    //订单预创建
    @Override
    public void preConfirmSuccess(JsonMap jsonMapData) {

        originAmount = jsonMapData.getString("originAmount");
        originServiceAmount = jsonMapData.getString("originServiceAmount");
        targetAccount = jsonMapData.getString("targetAccount");
        originCurrency = jsonMapData.getString("originCurrency");
        remark = jsonMapData.getString("remark");

        number.setText(originAmount);
        originalamount.setText(originAmount);
        serviceAmount.setText(originServiceAmount);
        targetAcc.setText(targetAccount);
        originCurr.setText(originCurrency);
        remarks.setText(remark);

    }

    //订单
    @Override
    public void getOrderDetails(JsonMap jsonMap) {


    }
}